package app

import (
	// "fmt"
	// "time"

	"github.com/go-apibox/api"
	"github.com/go-apibox/filter"
)

func ActionDeviceGroupList(c *api.Context) interface{} {
	params := c.NewParams()
	params.AddPagination().AddOrderBy("CreateTime", "desc", []string{"CreateTime"}, false)
	params.Add("DeviceGroupId", filter.EmptyToNil(), filter.Uint32())
	params.Add("DeviceGroupName", filter.EmptyToNil(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	list := make([]*DeviceGroup, 0, 10)
	return api.List(c, &list, params, map[string]string{
		"DeviceGroupName": "like",
		"Desc":            "like",
	})
}

func ActionDeviceGroupCreate(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("DeviceGroupName", filter.EmptyToNil(), filter.Required(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	return api.Create(c, new(DeviceGroup), params)
}

func ActionDeviceGroupUpdate(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("DeviceGroupId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	params.Add("DeviceGroupName", filter.EmptyToNil(), filter.String())
	params.Add("Desc", filter.EmptyToNil(), filter.String())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	return api.Update(c, new(DeviceGroup), params)
}

func ActionDeviceGroupDetail(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("DeviceGroupId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	return api.Detail(c, new(DeviceGroup), params)
}

func ActionDeviceGroupDelete(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("DeviceGroupId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}

	dbSession := app.engine.NewSession()
	if err := dbSession.Begin(); err != nil {
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}

	data := api.SessionDelete(c, dbSession, new(DeviceGroup), params)
	if api.IsError(data) {
		dbSession.Rollback()
		return data
	}
	if _, err := dbSession.Where("device_group_id =?", params.GetUint32("DeviceGroupId")).Delete(new(Device)); err != nil {
		dbSession.Rollback()
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}
	if err := dbSession.Commit(); err != nil {
		app.logger.Error(err)
		return c.Error.New(api.ErrorInternalError, "Connect db faield")
	}
	return data
}

func ActionDeviceGroupGetProduct(c *api.Context) interface{} {
	params := c.NewParams()
	params.Add("DeviceGroupId", filter.EmptyToNil(), filter.Required(), filter.Uint32())
	if apiErr := params.Parse(); apiErr != nil {
		return apiErr
	}
	id := params.GetUint32("DeviceGroupId")
	groupProductMapMux.RLock()
	defer groupProductMapMux.RUnlock()

	g, has := groupProductMap[id]
	if !has {
		return c.Error.New(api.ErrorInternalError, "xx")
	}
	return g
}
